//
//  LoginController.swift
//  baihuiju
//
//  Created by 赵家彬 on 2018/5/24.
//  Copyright © 2018年 赵家彬. All rights reserved.
//

import UIKit
import RxSwift
import RxCocoa
import IQKeyboardManager


class LoginController: CBBaseViewController {
    
    @IBOutlet weak var btnClose: UIButton!
    
    @IBOutlet weak var tfUsername: UITextField!
    
    @IBOutlet weak var tfPassword: UITextField!
    
    @IBOutlet weak var btnLogin: UIButton!
    
    @IBOutlet weak var lblUsernameTips: UILabel!
    
    @IBOutlet weak var lblPasswordTips: UILabel!
    
    @IBOutlet weak var vwLogin: UIView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        btnClose.rx.tap
            .subscribe(onNext: { [weak self] in
                self?.dismiss(animated: true, completion: nil)
            })
            .disposed(by: disposeBag);
        
        let loginVm = LoginViewModel(username: tfUsername.rx.text.orEmpty.asObservable(), password: tfPassword.rx.text.orEmpty.asObservable(), loginTaps: btnLogin.rx.tap.asObservable())
        
        loginVm.validatedUsername
            .bind(to: lblUsernameTips.rx.validationResult)
            .disposed(by: disposeBag)
        loginVm.validatedPassword
            .bind(to: lblPasswordTips.rx.validationResult)
            .disposed(by: disposeBag)
        loginVm.signinEnable
            .subscribe(onNext:{ [weak self] valid in
                self?.btnLogin.isEnabled = valid
                self?.btnLogin.alpha = valid ? 1.0 : 0.5
            })
            .disposed(by: disposeBag)
        loginVm.signIn.subscribe(onNext:{ [weak self] (isCanLogin) in
            SVProgressHUD.dismiss()
            if isCanLogin {
                self?.dismiss(animated: true, completion: nil)
                //发送刷新个人中心页面通知
                NotificationCenter.default.post(name: NSNotification.Name.init(rawValue: "Flush_User_Info"), object: nil)
            }
        }).disposed(by: disposeBag)
        
        
        //键盘弹出
        NotificationCenter.default.rx.notification(Notification.Name.UIKeyboardWillShow)
            .subscribe(onNext: { [unowned self] notification in
                let keyboardHt:CGFloat = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? CGRect)?.size.height ?? 0.0
                self.vwLogin.snp.remakeConstraints({ (make) in
                    make.centerX.equalToSuperview()
                    make.top.equalToSuperview().offset(S_Height - keyboardHt - 230.sp)
                    make.size.equalTo(CGSize(width: 272.sp, height: 261.sp))
                })
            })
            .disposed(by: disposeBag)
        //键盘收起
        NotificationCenter.default.rx.notification(Notification.Name.UIKeyboardWillHide)
            .subscribe(onNext: { [unowned self] _ in
                self.vwLogin.snp.remakeConstraints({ (make) in
                    make.center.equalToSuperview()
                    make.size.equalTo(CGSize(width: 272.sp, height: 261.sp))
                })
            })
            .disposed(by: disposeBag)
        
        let tapBack = UITapGestureRecognizer()
        tapBack.rx.event.subscribe({ [weak self] _ in
            self?.view.endEditing(true)
        }).disposed(by: disposeBag)
        view.addGestureRecognizer(tapBack)
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.navigationController?.isNavigationBarHidden = true
        IQKeyboardManager.shared().isEnabled = false
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        self.navigationController?.isNavigationBarHidden = false
        IQKeyboardManager.shared().isEnabled = true
    }
    
}





